package com.browsesoft;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.browsesoft.dbtools.ConnectionPool;
import com.browsesoft.dbtools.DBToolsRMIP;
import com.browsesoft.htmlcomponent.TemplateManager;
import com.browsesoft.oa.ExecuteTimeTask;
import com.browsesoft.synchronserver.SynchronServerRMI;

/**
 * 系统服务类，用于装载静态内容。
 * 
 * @author Browsesoft
 * @version 1.0
 */
public class Server {
	private static boolean first = true;

	/**
	 * 实体管理器
	 */
	private static EntityManagerRMI entityManager = null;

	/**
	 * 装载静态内容
	 * 
	 * @throws Exception
	 */
	public static void load() throws Exception {
		if (first) {
			System.out.println("begin load.");
			first = false;
			// 装载属性文件
			PropertiesService.load("/properties.xml");
			System.out.println("properties loaded.");
			// 装载数据库连接
			ConnectionPool.load();
			System.out.println("connection pool load.");
			// 管理器的接口
			ManagerGroup group = new ManagerGroup();
			group.load();
			// 装载资源所对应的权限类型信息
			RightTypeManager.load();
			System.out.println("right loaded.");
			// 装载系统的守护线程
			loadThreads();
			// 装载序列产生器
			SequenceFind.loadID();
			if (PropertiesService.getDocument()
					.getElementsByTagName("timetask").getLength() > 0) {
				// 启动后台监控任务线程
				startTimerTaskThread();
			}

		}
	}

	private static void startTimerTaskThread() {
		try {
			NodeList nl = PropertiesService.getDocument().getElementsByTagName(
					"timetask");
			for (int i = 0; i < nl.getLength(); i++) {
				Element e = (Element) nl.item(i);
				String className = e.getAttribute("classname");
				if (className == null || className.equals("")) {
					throw new RuntimeException("未设置className属性");
				}
				ExecuteTimeTask ett = (ExecuteTimeTask) Class
						.forName(className).newInstance();
				ett.start();
			}
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}

	}

	/**
	 * 启动rmi服务
	 */
	private static void rmiLoad() throws Exception {
		// 得到流程定义的路径
		String port = PropertiesService.getProperty("rmi", "port");
		// 注册RMI服务
		Registry r = LocateRegistry.createRegistry(Integer.parseInt(port));
		// 管理器的接口
		ManagerGroup group = new ManagerGroup();
		group.load();
		r.bind("ManagerGroup", group);
		// 实体绑定
		EntityManager eManager = (EntityManager) group.getManager("entity");
		r.bind("EntityManager", eManager);
		// try
		// {
		// String rmiAdd = PropertiesService.getProperty("remote", "address");
		// entityManager = (EntityManagerRMI) Naming.lookup("rmi://" + rmiAdd +
		// "/EntityManager");
		// }
		// catch (Exception ee)
		// {
		// ee.printStackTrace();
		// }
		r.bind("TemplateManager", (TemplateManager) group
				.getManager("template"));
		// 生成id接口
		SequenceImpl s = new SequenceImpl();
		r.bind("sequence", s);
		// 绑定rmi服务
		r.bind("DBToolsRMIP", new DBToolsRMIP());
		r.bind("PropertiesServiceRMI", new PropertiesServiceRMI());
		// 同步服务注册
		String synchronServer = PropertiesService.getProperty("SynchronServer",
				"classname");
		if (synchronServer != null && !synchronServer.equals("")) {
			SynchronServerRMI ssr = (SynchronServerRMI) Class.forName(
					synchronServer).newInstance();
			r.bind("SynchronServer", ssr);
		}
	}

	/**
	 * 装载系统守护线程
	 */
	private static void loadThreads() {
		String name = null;
		try {
			// 得到属性文件
			Document doc = PropertiesService.getDocument();
			// 找到所有线程标记
			NodeList threads = doc.getElementsByTagName("thread");
			// 对于每一个线程标记
			for (int i = 0; i < threads.getLength(); i++) {
				// 得到线程名
				Element thread = (Element) threads.item(i);
				name = thread.getAttribute("name");
				// 产生线程对象
				Runnable run = (Runnable) Class.forName(name).newInstance();
				new Thread(run).start();
			}
		} catch (Exception e) {
			throw new RuntimeException("产生线程错误，线程名:" + name, e);
		}
	}

	public static EntityManagerRMI getEntityManagerRMI() {
		return entityManager;
	}

}